﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace PrisonDefence.Models
{
    public class Map
    {
        private int[,] map = new int[,]
        {
            { 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, }, 
            { 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, },
            { 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, }, 
            { 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, }, 
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, },
            { 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, }, 
            { 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, }, 
            { 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, }, 
            { 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, }, 
            { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, }, 
        };

        /// <summary>
        /// The width of the map.
        /// </summary>
        public int Width
        {
            get { return map.GetLength(0); }
        }
        /// <summary>
        /// The height of the map.
        /// </summary>
        public int Height
        {
            get { return map.GetLength(1); }
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        public Map(Texture2D levelMap)
        {
            Initialize(levelMap);
        }

        private void Initialize(Texture2D levelMap)
        {
            Color[] colorData = new Color[levelMap.Width * levelMap.Height];
            map = new int[levelMap.Width, levelMap.Height];

            levelMap.GetData<Color>(colorData);

            for (int x = 0; x < levelMap.Width; x++)
            {
                for (int y = 0; y < levelMap.Height; y++)
                {
                    Color c = colorData[y * levelMap.Width + x];
                    if (c == Color.White)
                        map[x, y] = 0;
                    else if (c.R > 0 && c.G > 0 && c.B > 0)
                        map[x, y] = 1;
                }
            }

#if false
            StringBuilder sb = new StringBuilder();
            for (int y = 0; y < levelMap.Height; y++)
            {
                for (int x = 0; x < levelMap.Width; x++) 
                {
                    sb.Append(map[x, y]);
                    sb.Append(", ");
                }
                sb.AppendLine();
            }

            System.Diagnostics.Debug.WriteLine(sb.ToString());
#endif
        }

        /// <summary>
        /// Returns the tile index for the given cell.
        /// </summary>
        public int GetIndex(int cellX, int cellY)
        {
            if (cellX < 0 || cellX > Width - 1 || cellY < 0 || cellY > Height - 1)
                return 0;

            return map[cellX, cellY];
        }

        internal void AddTower(int cellX, int cellY)
        {
            UpdateMap(cellX - 1, cellY - 1, -1);
            UpdateMap(cellX - 1, cellY, -1);
            UpdateMap(cellX - 1, cellY + 1, -1);

            UpdateMap(cellX, cellY - 1, -1);
            UpdateMap(cellX, cellY, 1);
            UpdateMap(cellX, cellY + 1, -1);

            UpdateMap(cellX + 1, cellY - 1, -1);
            UpdateMap(cellX + 1, cellY, -1);
            UpdateMap(cellX + 1, cellY + 1, -1);            
        }

        private void UpdateMap(int cellX, int cellY, int amount)
        {
            if (cellX < 0 || cellX > Width - 1 || cellY < 0 || cellY > Height - 1)
                return;

            if ((amount < 0 && map[cellY, cellX] <= 0) || (amount > 0 && map[cellY, cellX] >= 0))
                map[cellY, cellX] += amount;
        }
    }
}
